#include "linkqueue.h"


void linkqueue_init(link_pqueue *Q)
{
	//创建封装头尾指针的队列指针对象
	*Q = (link_pqueue)malloc(sizeof(link_queue));
	if (*Q == NULL) {
		perror("malloc");
		exit(1);
	}
	//创建链表队列的头节点让front指向头节点
	(*Q)->front = (list_pnode)malloc(sizeof(list_node));
	if ((*Q)->front == NULL) {
		perror("malloc");
		exit(1);
	}
	//让头节点的后继为NULL,让rear指向头节点
	(*Q)->front->next = NULL;
	(*Q)->rear = (*Q)->front;
	
	
}
bool isempty_linkqueue(link_pqueue pque)
{
	//当front和rear指针指向相同就是空队列
	if (pque->front == pque->rear)
		return true;
	else 
		return false;
}
void linkqueue_in(link_pqueue pque, list_pnode new)
{
	new->next = pque->rear->next;  //将尾节点后继保存到new节点后继
	pque->rear->next = new; //将new保存到尾节点后继
	//pque->rear = pque->rear->next; //将尾指针指向new节点
	pque->rear = new;
}
void linkqueue_out(link_pqueue pque, char** pdata)
{
	list_pnode tmp = NULL;
	//判断队列是否为空
	if (isempty_linkqueue(pque)) {
		printf("队列为空,无法出队!\n");
		return;
	}
	*pdata = pque->front->next->data;
	tmp = pque->front;
	pque->front = tmp->next;
	free(tmp);
}

void linkqueue_show(link_pqueue pque)
{
	list_pnode p;
	printf("----------------------------------------------------\n");
	printf("队头\t");
	for (p = pque->front->next; p != NULL; p = p->next)
		printf("%s\t", p->data);
	printf("队尾\n");
	printf("----------------------------------------------------\n");
}
